// 模拟用户余额（和首页保持一致，可后续优化为共享数据方式，比如用localStorage等）
let userBalance = 12345.67;

// 更新余额显示
function updateBalance() {
  const balance = document.getElementById('balance');
  if (balance) {
    balance.textContent = userBalance.toFixed(2);
  }
  const balanceShow = document.getElementById('balance-show');
  if (balanceShow) {
    balanceShow.textContent = userBalance.toFixed(2);
  }
}

function sendRedpack() {
  const recipient = document.getElementById('recipient').value;
  const amount = parseFloat(document.getElementById('redpack-amount').value);
  const blessing = document.getElementById('blessing').value || '恭喜发财，大吉大利！';

  if (!recipient || isNaN(amount) || amount < 0.01 || amount > 200) {
    alert('请填写正确信息（金额范围：0.01-200元）');
    return;
  }

  if (confirm(`向 ${recipient} 发送 ${amount} 元红包\n祝福语：${blessing}`)) {
    // 模拟扣除余额
    userBalance -= amount;
    updateBalance();
    alert('红包已发送！');
    window.history.back(); // 返回上一页
  }
}

// 页面加载时更新余额显示
window.onload = function () {
  updateBalance();
};